package cn.kduck.module.organization.dao.query;

import cn.kduck.core.dao.definition.BeanEntityDef;
import org.springframework.stereotype.Component;
import cn.kduck.core.dao.definition.BeanDefDepository;
import cn.kduck.core.dao.query.QueryCreator;
import cn.kduck.core.dao.query.QuerySupport;
import cn.kduck.core.dao.sqlbuilder.ConditionBuilder.ConditionType;
import cn.kduck.core.dao.sqlbuilder.SelectBuilder;
import cn.kduck.module.organization.service.impl.OrganizationServiceImpl;

import java.util.Map;

import static cn.kduck.module.organization.service.OrganizationService.CODE_ORGANIZATION;
import static cn.kduck.module.organization.service.OrganizationService.CODE_ORGANIZATION_USER;

/**
 * LiuHG
 */
@Component
public class OrganizationByUserId implements QueryCreator {

    @Override
    public QuerySupport createQuery(Map<String, Object> paramMap, BeanDefDepository depository) {
        BeanEntityDef orgEntityDef = depository.getEntityDef(CODE_ORGANIZATION);
        BeanEntityDef orgUserEntityDef = depository.getEntityDef(CODE_ORGANIZATION_USER);
//        SelectBuilder queryBuidler = new SelectBuilder("SELECT {*} FROM K_ORGANIZATION o,K_ORGANIZATION_USER ou WHERE o.ORG_ID=ou.ORG_ID",paramMap);
        SelectBuilder queryBuidler = new SelectBuilder(paramMap);
        queryBuidler.from("o",orgEntityDef).innerJoinOn("ou",orgUserEntityDef,"orgId").where()
                .and("ou.USER_ID", ConditionType.EQUALS,"userId").orderBy().asc("DATA_PATH").asc("ORDER_NUM");

        queryBuidler.bindFields("o",depository.getFieldDefList(CODE_ORGANIZATION));
        return queryBuidler.build();
    }
}
